package com.zmn.uac.client;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.saas.common.dro.app.AppInfoDRO;
import com.zmn.saas.dubbo.dto.app.AppDRO;
import com.zmn.saas.dubbo.interfaces.app.AppRemoteService;
import com.zmn.uac.UacException;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

/**
 * @title: 应用信息Dubbo接口
 * @author:Rananquan
 * @Date:2022/6/9 11:19
 */
@Slf4j
@Component
public class AppClient {

    @DubboReference(version = com.zmn.uuc.common.constant.DubboConsts.INTERFACE_VERSION, check = false)
    AppRemoteService appRemoteService;

    /**
     * 根据渠道获取应用信息
     * @param channelId 渠道ID
     * @return AppDRO
     */
    public AppDRO getAppByChannelId(Integer channelId){
        log.info("根据渠道获取应用信息入参：{}",channelId);
        ResponseDTO<AppDRO> appResponse = appRemoteService.getAppByChannelId(channelId);
        if(!appResponse.isSuccess()){
            log.error("根据渠道获取应用信息请求失败，失败原因：{}",appResponse.getMessage());
            throw new UacException(appResponse.getMessage());
        }
        AppDRO app = appResponse.getData();
        log.info("根据渠道获取应用信息返回response：{}", JSON.toJSONString(app));

        return app;
    }


    /**
     * 根据渠道id或者店铺id获取app明文信息
     * @param channelId 渠道ID
     * @param shopId 店铺ID
     * @return AppInfoDRO
     */
    public AppInfoDRO getShowAppByChannelIdOrShopId(Integer channelId,Integer shopId){
        log.info("根据渠道id或者店铺id获取app明文信息入参，channelId：{}，shopId：{}",channelId,shopId);
        ResponseDTO<AppInfoDRO> appInfoResponse = appRemoteService.getShowAppByChannelIdOrShopId(channelId, shopId);
        if(!appInfoResponse.isSuccess()){
            log.error("根据渠道id或者店铺id获取app明文信息请求失败，失败原因：{}",appInfoResponse.getMessage());
            throw new UacException(appInfoResponse.getMessage());
        }

        AppInfoDRO appInfo = appInfoResponse.getData();
        log.info("根据渠道id或者店铺id获取app明文信息返回response：{}",JSON.toJSONString(appInfo));

        return appInfo;
    }


    /**
     * 获取店铺配置三方账号app信息
     * @param shopId 店铺ID
     * @return AppDRO
     */
    public com.zmn.saas.common.dro.app.AppDRO getAppInfoByShopId(Integer shopId){
        log.info("获取店铺配置三方账号app信息入参：{}", shopId);
        ResponseDTO<com.zmn.saas.common.dro.app.AppDRO> appResponse = appRemoteService.getAppInfoByShopId(shopId);
        if(!appResponse.isSuccess()){
            log.error("获取店铺配置三方账号app信息请求失败，失败原因：{}",appResponse.getMessage());
            throw new UacException(appResponse.getMessage());
        }

        com.zmn.saas.common.dro.app.AppDRO app = appResponse.getData();
        log.info("获取店铺配置三方账号app信息返回response：{}",JSON.toJSONString(app));

        return app;
    }
}
